<template>
	<view>
		<view>
			<!-- 收货地址组件 -->
			<my-address :hasDefaultAddress="hasDefaultAddress" :hasAddr="hasAddr" :selectAddr="selectAddr"></my-address>
		</view>

		<view class="good-list">
			<view class="title">
				商品清单
			</view>

			<view class="list">
				<!-- 商品列表 -->
				<my-goods :showGoodsNum="true" :goodsList="orderList"></my-goods>
			</view>
			<!-- 红包折扣 -->
			<view class="discount">
				<view class="">
					可使用红包抵扣： <text class="discount-num">￥{{discountNum.toFixed(2)}}</text>
				</view>
				<label class="radio">
					<radio value="" :checked="isChecked" @click="useDiscount" color="#D80E0E" /><text></text>
				</label>
			</view>

			<!-- 备注区域 -->
			<view class="remark">
				<!-- <text class="remark-content">选填，如有特殊需求，请备注</text> -->
				备注<input type="text" @input="getInputValue" class="remark-content" placeholder="选填，如有特殊需求，请备注" />
			</view>

		</view>
		<!-- 结算组件 -->
		<my-settle :hideOrderRadio="true" :remarktext="remarktext" :discountNum="disCount" :orderList="orderList"
			:totalPrice="totalPrice" :showOrderSettleBtn="true" @sendMsg="getMsg"></my-settle>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapState,
		mapGetters
	} from 'vuex'
	export default {
		props: [],
		data() {
			return {
				// address: {}, // 默认收货地址
				orderList: [],
				totalPrice: 0, // 选中商品总价
				goodsDetail: {}, // 商品详情
				isChecked: false, // 是否选中红包
				remarktext: '', // 备注信息
				// address: {},
				discountNum: 0,
				discount: 0,
				openId: '',
				disCount: 0,
				goodsIds: [], // 商品的id
				floorPrice: [], // 底价
				price: [], // 价格
				addressString: {}, // 默认地址
				hasAddr: false, // 是否有地址
				hasDefaultAddress: false,
				isChanged: false, // 是否选了非默认地址
				selectAddr: false,
				addressList: [] // 收货地址列表
			};
		},
		onshow() {
			if (uni.getStorageSync('isChanged' == true)) {
				this.isChanged = true
			} else {
				this.isChanged = false
			}



		},
		onLoad(option) {

			
			// console.log(this.userInfo.hb, this.discountNum, option)

			if (option.num) {
				let goodsDetail = JSON.parse(uni.getStorageSync('detail'))
				// console.log(goodsDetail)
				let goods = {}
				goods.userId = this.userInfo._id
				goods.productId = goodsDetail._id
				goods.num = option.num
				goods.imgUrl = goodsDetail.imgUrl
				goods.productName = goodsDetail.name
				goods.price = goodsDetail.price
				goods.floorPrice = goodsDetail.floorPrice
				// console.log(1, goods)
				this.orderList.push(goods)
				this.totalPrice = goods.price * option.num
				this.discountNum = (goods.price - goods.floorPrice) * option.num
				if (this.discountNum > this.userInfo.hb) {
					this.discountNum = this.userInfo.hb
				}

				this.discountNum.toString()
			} else {
				this.orderList = JSON.parse(option.checkList)
				this.orderList.forEach(item => {
					// console.log(item)
					this.totalPrice += item.price * item.num
					// this.discountNum += (item.price - item.floorPrice) * item.num
					// 计算红包折扣
					this.goodsIds.push(item)
				})
				this.goodsIds.forEach(item => {
					console.log(item)
					// 获取商品详情
					this.getDetail(item)
				})
				uni.setStorageSync('totalPrice', this.totalPrice)
			}



		},


		onShow() {
			this.getAddressList()
			var pages = getCurrentPages(); // 当前页面
			var prevPage = pages[pages.length - 2].route; // 上一个页面
			console.log(prevPage)
			if (prevPage == 'pages/cart/cart' && JSON.stringify(this.address) == '{}') {
				this.selectAddr = false
			} else {
				this.selectAddr = true
			}

			// 更新用户信息
			if (uni.getStorageSync('openId')) {
				this.openId = uni.getStorageSync('openId')
				this.updateUserData()
			}



		},
		methods: {
			...mapMutations('m_user', ['updateAddress', 'updateUserInfo']),
			// 获取用户信息
			async updateUserData() {
				console.log(11)

				const {
					data: res
				} = await uni.$http.post('/user/getUserData', {
					openid: uni.getStorageSync('openId'),
					phone: uni.getStorageSync('phone'),
					nickname: this.userInfo.nickname,
					headImg: this.userInfo.headImg,
					recPhone: ''
				})
				console.log(res, 55)
				this.updateUserInfo(res.data)

			},
			getMsg(msg) {
       console.log(msg)
			 // uni.$showMsg(msg.slice(0,12)+'...'+'库存不足!')
			 // // uni.showModal({
			 // // 	title:msg
			 // // })
			 uni.showToast({
			 	title:msg.slice(0,15)+'...'+'商品库存不足!',
			 	duration:1500,
			 	icon:'none'
			 })
			},
			// 获取收货地址
			async getAddressList() {
				const {
					data: res
				} = await uni.$http.post('/cart/getAddressList', {
					userId: this.userInfo._id
				})
				console.log(res.data.length)
				this.addressList = res.data

				// 有收货地址
				if (res.data.length > 0) {
					this.addressString = res.data.filter(item => {
						return item.isDefault == '1'
					})
					// 有收货地址
					this.hasAddr = true
					// 有默认收货地址
					if (this.addressString.length != 0) {
						this.addressString = this.addressString[0]
						this.updateAddress(this.addressString)

						// uni.setStorageSync('defAddr',JSON.stringify(this.addressString))
						this.hasDefaultAddress = true

					} else {
						// 没有默认收货地址
						// this.updateAddress(res.data[0])
					}
				} else {
					// 没有收获地址
					this.selectAddr = false
					this.hasAddr = false
					for (let key in this.address) {
						delete this.address[key];
					}

				}
			},
			// 获取商品详情
			async getDetail(item) {
				const {
					data: res
				} = await uni.$http.post('/product/getProductDetails', {
					id: item.productId
				})
				console.log(res)
				this.floorPrice.push(res.data.floorPrice)
				this.price.push(res.data.price)
				let discount = (res.data.price - res.data.floorPrice) * item.num
				this.discountNum += discount
				if (this.discountNum > this.userInfo.hb) {
					this.discountNum = this.userInfo.hb
				}
				this.discountNum.toString()
			},

			// 使用红包折扣
			useDiscount() {
				this.isChecked = !this.isChecked
				if (this.isChecked) {
					// 使用了红包
					this.disCount = this.discountNum

					this.totalPrice -= this.discountNum

					return
				} else {
					// 未使用红包
					this.disCount = 0
					this.totalPrice += this.discountNum
					return
				}
			},
			// 获取备注的信息
			getInputValue(e) {
				clearTimeout(this.timerId)
				this.timerId = setTimeout(() => {
					this.remarktext = e.detail.value
					console.log(this.remarktext)
				}, 500)
			},
		},
		computed: {
			...mapState('m_user', ['address', 'userInfo']),
			...mapGetters('m_user', ['addressStr'])
		}
	}
</script>

<style lang="scss">
	.select-address {
		height: 117rpx;
		line-height: 117rpx;
		font-size: 36rpx;
		font-weight: 500;
		color: #333333;
		border-top: 1rpx solid #DCDCDC;
		border-bottom: 1rpx solid #E8E8E8;
		padding: 0 24rpx;
		display: flex;
		align-items: center;
		position: relative;

		.setAddr {
			width: 32rpx;
			height: 44rpx;
			vertical-align: middle;
			margin-right: 15rpx;
		}

		.arr-r {
			width: 15rpx;
			height: 28rpx;
			position: absolute;
			right: 25rpx;
		}
	}

	.good-list {
		height: 540rpx;

		.title {
			font-size: 32rpx;
			font-weight: bold;
			color: #333333;
			height: 94rpx;
			line-height: 110rpx;
			padding-left: 24rpx;
			border-bottom: 1px solid #E8E8E8;
		}
	}

	// 折扣
	.discount {
		height: 112rpx;
		line-height: 112rpx;
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
		box-sizing: border-box;
		padding-left: 24rpx;
		border-bottom: 1px solid #E8E8E8;
		display: flex;
		justify-content: space-between;
		padding-right: 20rpx;

		.discount-num {
			font-size: 32rpx;
			font-weight: bold;
			color: #FF1111;
		}
	}

	// 备注
	.remark {
		height: 106rpx;
		line-height: 106rpx;
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
		box-sizing: border-box;
		padding-left: 24rpx;
		border-bottom: 1px solid #E8E8E8;
		display: flex;
		align-items: center;
		// padding-bottom: 120rpx;
		margin-bottom: 300rpx;

		.remark-content {
			width: 600rpx;
			font-size: 24rpx;
			font-weight: 400;
			color: #666666;
			margin-left: 32rpx;
		}
	}
</style>
